#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 BidiX (http://bidix.info)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
"""
notewikiapp_client
******************
	HTTP/HTTPS Client to Notewiki App.
	
	Description:
		- Issues request to a Notewiki App
		- uses gae_client.py to issue request to Google App Engine 
 
	Usage as a command::
	
		notewikiapp_client.py {app} {method} {url} [{data}]
		
	Usage as a lib::

		client = Notewikiapp_Client(notewiki_app)		
		(response_code, response_body) = client.get_erticle(name)	
		(response_code, response_body) = client.post_article(name, title, text, tag)
		
	notewikiapp_client.config: to specify environment of NotewikiApp::

		[notewiki_app]
		scheme = HTTP
		domain = {domain}
		user = {address}
		password = {password}
		cookies_filename = cookies.txt

"""

import sys
import urllib2
from ConfigParser import ConfigParser
from urllib import urlencode
from gae_client import GAE_Client

class Notewikiapp_Client(GAE_Client):
	"""
	Extends GAE_Client.	
	"""
	config_filename = 'notewikiapp_client.config'
	server_root_url = '/'
	
	def __init__(self, notewikiapp, config_filename='notewikiapp_client.config'):
		config = ConfigParser()
		config.read(config_filename)
		self.app = notewikiapp
		self.scheme = config.get(notewikiapp, 'scheme')
		self.domain = config.get(notewikiapp, 'domain')
		self.user = config.get(notewikiapp, 'user')
		self.password = config.get(notewikiapp, 'password')
		self.cookies_filename = config.get(notewikiapp, 'cookies_filename')
		GAE_Client.__init__(self, self.user, self.password, self.domain, self.app, self.cookies_filename, local = False, scheme = self.scheme)
		
	def get_article(self, name):
		"""
		Get a Notewiki article. 
		
		Args:
			name (String): article_name with extension
		
		Returns:
			(response_code, response_body)
		"""
		try:
			(response_code, response_body) = self.request('GET', '/'+name)
		except urllib2.HTTPError, error:
			response_code = error.code
			response_body = error.msg
		return (response_code, response_body)

		
	def delete_article(self, name):
		"""
		Delete a Notewiki article.
		"""
		return self.request('GET', '/'+name+'?delete')

	def post_article(self, name, title, text, tag):
		"""
		Create or update a Notewiki article
		"""
		url = self.server_root_url+name
		body = urlencode({'name': name, 'title': title, 'text': text, 'tag': tag})
		return self.request('POST', url, body)

def main(argv):
	app = argv[1]
	method = argv[2]
	url = argv[3]
	data = None
	if len(argv) > 4:
		data = argv[4]
	app = Notewikiapp_Client(app)
	code, body = app.request(method, url, data)
	if code == 200:
		print body
	else:
		print "Return code : %s"%code
		print "body : \n%s"%body

if __name__ == '__main__':
    main(sys.argv)